package thread;

import java.util.Random;
class Counter{
    int evenSum;
    int oddSum;
}
public class ThreadHomeWork1 {

    public static void main(String[] args)throws InterruptedException {
        int[] num = new int[1000_0000];
        Random random = new Random();
        Counter counter = new Counter();
        for (int i = 0; i < num.length; i++) {
            num[i] = random.nextInt(100)+1;
        }
        Long start =System.nanoTime();
        Thread t1 = new Thread(() -> {
            int evenSum = 0;
            for (int i = 0; i < num.length; i=i+2) {
                evenSum +=num[i];
            }
            counter.evenSum=evenSum;
        },"子线程");
        t1.start();
        for (int i = 1; i < num.length; i+=2) {
            counter.oddSum +=num[i];
        }
        t1.join();
        System.out.println("总和为 : " + (counter.evenSum+counter.oddSum));
        Long end = System.nanoTime();
        System.out.println("总耗时为 : " +((end-start)*1.0/1000/1000)+"ms");
    }
}
